package com.deryun.tyl.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.json.HTTP;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.deryun.tyl.bean.Contact;
import com.deryun.tyl.bean.ContentMessage;
import com.deryun.tyl.bean.GroupMessage;
import com.deryun.tyl.bean.IndexImage;
import com.deryun.tyl.bean.Platform;
import com.deryun.tyl.bean.Policy;
import com.deryun.tyl.bean.Publicity;
import com.deryun.tyl.bean.Purchasing;
import com.deryun.tyl.bean.Standard;
import com.deryun.tyl.bean.User;
import com.deryun.tyl.bean.tempBean.FriendConnect;
import com.deryun.tyl.dao.impl.ContactDaoImpl;
import com.deryun.tyl.dao.impl.GroupMessageDaoImpl;
import com.deryun.tyl.dao.impl.PlatformDaoImpl;
import com.deryun.tyl.dao.impl.StandardDaoImpl;
import com.deryun.tyl.dao.impl.UserDaoImpl;
import com.deryun.tyl.service.impl.ContentMessageServiceImpl;
import com.deryun.tyl.service.impl.IndexImageServiceImpl;
import com.deryun.tyl.service.impl.PolicyServiceImpl;
import com.deryun.tyl.service.impl.PublicityServiceImpl;
import com.deryun.tyl.service.impl.PurchasingServiceImpl;
import com.deryun.tyl.service.impl.UserServiceImpl;



@Controller
@RequestMapping("civil")
public class CivilFrontPageController {
	
	@Resource(name="policyServiceImpl")
	private PolicyServiceImpl policyServiceImpl;
	
	@Resource(name="standardDaoImpl")
	private StandardDaoImpl standardDaoImpl;
	
	@Resource(name="purchasingServiceImpl")
	private PurchasingServiceImpl purchasingServiceImpl;
	
	@Resource(name="publicityServiceImpl")
	private PublicityServiceImpl publicityServiceImpl;
	
	@Resource(name="contentMessageServiceImpl")
	private ContentMessageServiceImpl contentMessageSI;
	
	@Resource(name="indexImageServiceImpl")
	private IndexImageServiceImpl indexImageSI;
	
	@Resource(name="groupMessageDaoImpl")
	private GroupMessageDaoImpl dao;
	
	@Resource(name = "userDao")
	private UserDaoImpl userDao;
	
	@Resource(name="contactDaoImpl")
	private ContactDaoImpl contactDaoImpl;
	
	@Resource(name="platformDaoImpl")
	private PlatformDaoImpl platformDaoImpl;
	
	

	/*首页*/
	@RequestMapping("/homepage")
	public String homepage(HttpServletRequest request){
		List<ContentMessage> message = contentMessageSI.getSixCM();
		request.setAttribute("message", message);
		List<IndexImage> images = indexImageSI.getAll();
		request.setAttribute("images", images);
		List<FriendConnect> messages = contentMessageSI.getFC();
		request.setAttribute("fc", messages);
		List<ContentMessage> product = contentMessageSI.getProductType();
		request.setAttribute("pd", product);
		List<User> supplier = userDao.getFrontSupplier();
		request.setAttribute("supplier", supplier);
		return "civil_service/homepage";
	}
	
	/*专题新闻*/
	@RequestMapping("/specialNews")
	public String specialNews(){
		return "civil_service/special_news";
	}
	
	/*行业动态*/
	@RequestMapping("/industryDynamic")
	public String industryDynamic(){
		return "civil_service/industry_dynamic";
	}
	
	/*政策法规*/
	@RequestMapping("/policiesRegulations")
	public String policiesRegulations(HttpServletRequest request){
		List<Policy> policies = policyServiceImpl.getAllPolicy();
		List<Policy> backPolicies = new ArrayList<Policy>();
		for(Policy c : policies) 
		{
			if(c.getResult() .trim().equals("1"))
			{
				backPolicies.add(c);
			}
		}
		request.setAttribute("policies", backPolicies);
		return "civil_service/policies_regulations";
	}
	
	/*标准规范*/
	@RequestMapping("/standardSpecification")
	public String standardSpecification(HttpServletRequest request){
		List<Standard> standards = standardDaoImpl.getAllStandard();
		List<Standard> backPolicies = new ArrayList<Standard>();
		for(Standard c : standards) 
		{
			if(c.getResult() .trim().equals("1"))
			{
				backPolicies.add(c);
			}
		}
		request.setAttribute("standards", backPolicies);
		return "civil_service/standard_specification";
	}
	
	/*信息公开*/
	@RequestMapping("/informationDisclosure")
	public String informationDisclosure(HttpServletRequest request){
		List<Publicity> publicities = publicityServiceImpl.getAllPublicity();
		List<Publicity> backPolicies = new ArrayList<Publicity>();
		for(Publicity c : publicities) 
		{
			if(c.getResult() .trim().equals("1"))
			{
				backPolicies.add(c);
			}
		}
		
		request.setAttribute("publicities", backPolicies);
		return "civil_service/information_disclosure";
	}
	
	/*采购资讯*/
	@RequestMapping("/purchasingInformation")
	public String purchasingInformation(HttpServletRequest request){
		List<Purchasing> purchasings = purchasingServiceImpl.getAllPurchasing();
		List<Purchasing> backPolicies = new ArrayList<Purchasing>();
		for(Purchasing c : purchasings) 
		{
			if(c.getResult() .trim().equals("1"))
			{
				backPolicies.add(c);
			}
		}
		request.setAttribute("purchasing", backPolicies);
		return "civil_service/purchasing_information";
	}
	
	/*文章详情*/
	@RequestMapping("/articleDetails")
	public String articleDetails(){
		return "civil_service/article_details";
	}
	
	/*采购公告*/
	@RequestMapping("/purchaseAnnouncement")
	public String purchaseAnnouncement(){
		return "civil_service/purchase_announcement";
	}
	
	/*注册*/
	@RequestMapping("/register")
	public String register(){
		return "civil_service/register_step1";
	}
	
	/*信用融资*/
	@RequestMapping("/creditFinancing")
	public String creditFinancing(HttpServletRequest request){
		List<ContentMessage> messages = contentMessageSI.getSixRZ();
		request.setAttribute("message", messages);
		return "civil_service/credit_financing";
	}
	
	/*信用融资子页面*/
	@RequestMapping("/creditFinancingZ")
	public String creditFinancingZ(){
		return "civil_service/credit_financing_z";
	}
	
	/*办事指南*/
	@RequestMapping("/lawGuide")
	public String lawGuide(){
		return "civil_service/law_guide";
	}
	
	/*联系方式*/
	@RequestMapping("/contactInformation")
	public String contactInformation(HttpServletRequest request){
		List<Contact> messages = contactDaoImpl.getAllContact();
		request.setAttribute("message", messages);
		Platform message = platformDaoImpl.getPlatform();
		request.setAttribute("platform", message);
		return "civil_service/contact_information";
	}
	
	@RequestMapping("/getDetail")
	public String getDetail(HttpServletRequest request){
		return "";
	}
	
	/*一键通活动*/
	@RequestMapping("/AKeyActivities")
	public String  AKeyActivities() {
		return "civil_service/a_key_activities";
	}
	
	/*一键通产品/服务供应商*/
	@RequestMapping("/AKeyServiceProvider")
	public String  AKeyServiceProvider() {
		return "civil_service/a_key_service_provider";
	}
	
	//第一步
	@RequestMapping("/register1")
	public String newRegister1(){
		return "civil_service/register_step1";
	}
	//第二步
	@RequestMapping("/register2")
	public String newRegister2(){
		return "civil_service/register_step2";
	}
	//第三步
	@RequestMapping("/register3")
	public String newRegister3(){
		return "civil_service/register_step3";
	}
	//第四步
	@RequestMapping("/register4")
	public String newRegister4(){
		return "civil_service/register_step4";
	}
	
	//前台查询内容
	@RequestMapping("/searchCM")
	public String searchCM(@RequestParam("plate") String plate,@RequestParam("title") String title, HttpServletRequest request) throws UnsupportedEncodingException{
		//String platet = new String(request.getParameter("plate").getBytes("iso-8859-1"), "utf-8"); 
		String platet = plate;
		//String titlet = new String(request.getParameter("title").getBytes("iso-8859-1"), "utf-8"); 
		String titlet = title;
		String ttitle = "%"+titlet+"%";
		List<ContentMessage> message = contentMessageSI.searchCM(platet, ttitle);
		request.setAttribute("message", message);
		return "civil_service/messge_search";
	}
	
	//前台查看详细内容
	@RequestMapping("/getCM")
	public String getCM(@RequestParam("id") int id,HttpServletRequest request){
		ContentMessage contentMessage = contentMessageSI.getCM(id);
		request.setAttribute("message", contentMessage);
		return "civil_service/article_details";
	}
	
	/*一键通服务/产品*/
	@RequestMapping("/aKeyServiceProduct")
	public String  aKeyServiceProduct() {
		return "civil_service/a_key_service_product";
	}
	
	/*一键通首页*/
	@RequestMapping("/aKeyHomePage")
	public String  aKeyHomePage(HttpServletRequest request) {
		ContentMessage message1 =  contentMessageSI.getOnekeyInfo();
		request.getSession().setAttribute("oneKeyPic", message1);
		
		List<ContentMessage> message = contentMessageSI.getSixKX();
		request.setAttribute("kuaixun", message);
		
		List<IndexImage> images = indexImageSI.getKuaiXun();
		request.setAttribute("kuaixunpic", images);
		
		List<GroupMessage> list = dao.getAllContact();
		request.setAttribute("groupList", list);
		
		List<User> user = userDao.getOneKeySupplier();
		request.setAttribute("supplier", user);
		
		return "civil_service/a_key_homepage";
	}
	
	//前往一键通介绍
	@RequestMapping("/toOnekeyInfo")
	public String  toOnekeyInfo(HttpServletRequest request) {
		return "civil_service/activity_info";
	}
	
	//获取内容通过版块
	@RequestMapping("/getCMByPlate")
	public String getCMsByPlate(@RequestParam("plate") String plate,HttpServletRequest request) throws UnsupportedEncodingException{
		// String platet = new String(plate.getBytes("iso-8859-1"), "utf-8"); 
		String platet = plate;
		List<ContentMessage> message = contentMessageSI.getCMByPlate(platet);
		System.out.println(message);
		request.setAttribute("message", message);
		if (platet.equals("一键通快讯")) {
			return "civil_service/onekey_list_page";
		}
		if (platet.equals("政策法规")) {
			return "civil_service/policies_regulations";
		}
		if (platet.equals("标准规范")) {
			return "civil_service/standard_specification";
		}
		if (platet.equals("专题新闻")) {
			return "civil_service/special_news";
		}
		if (platet.equals("采购公告")) {
			return "civil_service/purchase_announcement";
		}
		if (platet.equals("采购结果公告")) {
			return "civil_service/purchase_announcement_result";
		}
		if (platet.equals("一键通产品")) {
			return "civil_service/product_list";
		}
		if (platet.equals("一键通活动")) {
			return "civil_service/activity_list";
		}
		if (platet.equals("融资动态")) {
			return "civil_service/financing_list";
		}
		if (platet.equals("融资产品")) {
			return "civil_service/financing_product_list";
		}
		//TODO
		return "civil_service/onekey_list_page";
	}
	
	//获取所有用户
	@RequestMapping("getAllUser")
	public String getAllUser(HttpServletRequest request){
		List<User> users = userDao.getAllUser();
		request.setAttribute("message", users);
		return "civil_service/product_service_suppliers";
	}
}
